package com.sky.web.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    // 新增套餐
    @CacheEvict(value = "setmealCache",key ="#setmealDTO.categoryId" )
    @PostMapping
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);
        return Result.success();
    }

    // 分页
    @GetMapping("/page")
    public Result getPage(SetmealPageDTO setmealPageDTO) {
        PageResult pageResult = setmealService.getPage(setmealPageDTO);
        return Result.success(pageResult);
    }

    // 回显套餐
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id")Long id){
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }
    // 修改套餐
    @PutMapping
    @CacheEvict(value = "setmealCache",key ="#setmealDTO.categoryId")
    public Result updateById(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateById(setmealDTO);
        return Result.success();
    }
    // 套餐批量删除
    @DeleteMapping
    @CacheEvict(value = "setmealCache",key ="#setmealDTO.categoryId")
    public Result deleteBatch(@RequestParam List<Long> ids){
        setmealService.deleteBatch(ids);
        return Result.success();
    }
    // 启售停售套餐
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",key ="#setmealDTO.categoryId")
    public Result startOrStop(@PathVariable("status") Integer status,@RequestParam Long id){
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealService.startOrStop(setmeal);
        return Result.success();
    }
}
